var express = require('express');
var router = express.Router(); 
var sql = require('./../sql/index');
var Pro = require('./../sql/pro');
var xlsx = require('node-xlsx');
var uuid = require('node-uuid');
// var utils = require('./../utils');
var filestr = "E:/千锋/三阶段/week2-4/vue-smartisan/node/node-test/shop.xlsx"

router.get('/import', (req, res, next) => {
    let obj = xlsx.parse(filestr)[0].data;
    let arr = [];
    console.log(obj)
    obj.map((item, index) => {
        if (index != 0 && item.length !=0) {
            arr.push({
                proid: 'pro_' + uuid.v1(),
                type: item[0],
                classification: item[1],
                brand: item[2],
                proimg: item[3],
                price: item[4],
                title: item[5],
           }) 
        }
    })
    sql.insert(Pro, arr).then(() => {
        res.send(arr)
    })
})

router.get('/', (req, res, next) => {
    let { pageCode, limitNum } = req.query;
    pageCode = pageCode * 1 || 0;
    limitNum = limitNum * 1 || 10;
    sql.paging(Pro, {}, { _id: 0 },  limitNum,pageCode).then((data) => {
        res.send({
            code: '200',
            mess: '查询列表成功',
            length: data.length,//总共查到多少条
            data:data
        })
    })
})
router.get('/type', (req, res, next) => {
    let { type } = req.query;
    type = type || 'type';
    sql.distinct(Pro, type).then((data) => {
        res.send({
            code: '200',
            mess: '获取类型成功',
            data:data
            
        })
    })
})
router.get('/brand', (req, res, next) => {
    let { type } = req.query;
    sql.find(Pro, {type:type}).then((data) => {
        res.send({
            code: '200',
            mess: '获取商品成功',
            data:data
            
        })
    })
})
router.get('/shop', (req, res, next) => {
  let { classification } = req.query;
  sql.find(Pro, {classification:classification}).then((data) => {
      res.send({
          code: '200',
          mess: '获取商品成功',
          data:data
          
      })
  })
})
router.get('/detuil', (req, res, next) => {
    let { proid } = req.query;
    sql.find(Pro, { proid }, { _id: 0 }).then(data => {
        res.send({
            code: '200',
            mess: '查询数据成功',
            data:data[0]
        })
    })
})
// 获取分类类型对应的品牌
// router.get('/category', (req, res, next) => {
//     let { type } = req.query
//     sql.find(Pro, { type }, {_id: 0, brand:1, barndimg: 1}).then(data => {
//       // 数组去重 https://www.cnblogs.com/le220/p/9130656.html
//       let obj = {}
//       // 利用reduce方法遍历数组,reduce第一个参数是遍历需要执行的函数，第二个参数是item的初始值
//       // [{brand: '1', barndimg: '11'}, {brand: '2', barndimg: '22'},{brand: '1', barndimg: '11'}]
//       // [{brand: '1', barndimg: '11'}, {brand: '2', barndimg: '22'}]
//       data = data.reduce((item, next) => {
//         obj[next.brand] ? '' : obj[next.brand] = true && item.push(next)
//         return item
//       }, [])
//       res.send({
//         code: '200',
//         message: '获取分类类型列表',
//         data: data
//       })
//     })
//   })
  
  // 获取品牌类型对应的产品
  // router.get('/brandcategory', (req, res, next) => {
  //   let { brand } = req.query
  //   sql.find(Pro, { brand: brand }, {_id: 0}).then(data => {
      
  //     res.send({
  //       code: '200',
  //       message: '获取品牌分类列表',
  //       data: data
  //     })
  //   })
  // })
  
  // 搜索
  router.get('/search', (req, res, next) => {
    let { text } = req.query
    sql.find(Pro, { type: eval('/' + text + '/') }, {_id: 0}).then(data => {
      
      res.send({
        code: '200',
        message: '搜索列表',
        data: data
      })
    })
  })
module.exports = router